#处理参数，规范化参数
ARGS=`getopt -a -o p:P:b:h::w::s:: --long port:,product:,build::,help::,webkit2::,shared:: -- "$@"`
#echo $ARGS
#将规范化后的命令行参数分配至位置参数（$1,$2,...)
eval set -- "${ARGS}"

declare -A TOOLCHAINS #key/value dict ,key is platform,value is toolchain,key must be uppercase
TOOLCHAINS["SIGMA"]=$HOME/cdroid/cmake/ssd202-mtitoolchain.cmake

declare -A DEPLIBS #key/value dict,key is platform,value is deplibs dir in vcpkg,key must be uppercase
DEPLIBS["X64"]=/usr/lib/x86_64-linux-gnu/pkgconfig:$HOME/vcpkg/installed/x64-linux-dynamic
DEPLIBS["SIGMA"]=$HOME/vcpkg/installed/arm-linux-dynamic

SHOWHELP=false
PORT="CDROID"
PRODUCT="x64"
BUILD_TYPE="Release"
WEBKIT="-DENABLE_WEBKIT=ON -DENABLE_WEBKIT2=OFF"
USE_WEBKIT2=false
SHARED=OFF

while :
do
   case $1 in 
	-p|--port)
		PORT=$2
		echo "port=$PORT"
		shift
		;;
        -P|--product)
		PRODUCT=$2
		echo "product=$PRODUCT"
		shift
		;;
	-b|--build)
		BUILD_TYPE=$2
		echo "build=$BUILD_TYPE"
		shift
		;;
	-w|--webkit2)
		WEBKIT="-DENABLE_WEBKIT=OFF -DENABLE_WEBKIT2=ON"
		USE_WEBKIT2=true
		echo "webkit2"
		shift
		;;
	-h|--help)
		SHOWHELP=true
		echo "showhelp"
		shift
		;;
	-s|--shared)
		SHARED=ON
		echo "shared"
		shift
		;;
	--)
		shift
		break
		;;
	*)
		break
		;;
   esac
   shift
done

PORT=${PORT^^}
PRODUCT=${PRODUCT^^}
BUILD_TYPE=${BUILD_TYPE,,}
BUILD_TYPE=${BUILD_TYPE^}
CDROID_DIR=$HOME/cdroid/out${PRODUCT}-${BUILD_TYPE}

echo "port=$PORT"
echo "product=$PRODUCT ${PRODUCT,,}"
echo "showhelp=$SHOWHELP"
echo "build=${BUILD_TYPE}"
echo "webkit=${WEBKIT}"
echo "shared=${SHARED}"

if [ "$PRODUCT" = "X64" ]; then 
    TOOLCHAIN_FILE=""
else
    TOOLCHAIN_FILE="-DCMAKE_TOOLCHAIN_FILE=${TOOLCHAINS[${PRODUCT}]}"
fi

if [ "$PORT" = "CDROID" ];then
    OUTDIR=outCD-${PRODUCT}-${BUILD_TYPE}
else
    OUTDIR=out-${PRODUCT}-${BUILD_TYPE}
fi

DEPLIBS_DIR=${DEPLIBS[$PRODUCT]}

if [ "$PRODUCT" != "X64" ]; then
    echo "TOOLCHAIN_FILE=${TOOLCHAIN_FILE}"
fi

if [ ${SHOWHELP,,} = "true" ];then
    echo ""
    echo "Usage: $0 [options] $#"
    echo "-P|--product [x64,sigma]"
    echo "-p|--port[GTK,CDROID] Select Webkit PORT"
    echo "-b|--build[Debug,Release,RelWithDebInfo]"
    echo "-h|--help Show Help Info,Usage"
    echo "-s|--shared Compile to shared library"
    echo "-w|--webkit2 Use Webkit2"
    echo ""
    exit
fi	

CDROID_DIR=$HOME/cdroid/out${PRODUCT}-${BUILD_TYPE}
if [ ! -d $CDROID_DIR ];then
  CDROID_DIR=$HOME/cdroid/out${PRODUCT}-Release
fi

if [ ${USE_WEBKIT2,,} = "true" ];then
    OUTDIR=${OUTDIR}2
fi

if [[ ${SHARED,,} = "off" || ${SHARED,,} = "false" ]];then
   SHARED="-DSHARED_CORE=OFF -DJavaScriptCore_LIBRARY_TYPE=STATIC "
   SHARED=${SHARED}" -DWebCoreCore_LIBRARY_TYPE=STATIC -DWebKit_LIBRARY_TYPE=STATIC"
   echo "SHARED=${SHARED}"
fi

mkdir -p ${OUTDIR}
pushd ${OUTDIR}

export PKG_CONFIG_PATH=$DEPLIBS_DIR/lib/pkgconfig:$CDROID_DIR/lib/pkgconfig
export PKG_CONFIG_LIBDIR=$DEPLIBS_DIR/lib/pkgconfig:$CDROID_DIR/lib/pkgconfig
echo "PKG_CONFIG_PATH=${PKG_CONFIG_PATH}"
#--debug-output
#-DCMAKE_PREFIX_PATH=$CDROID_DIR/lib/pkgconfig \
cmake -DPORT=$PORT  \
        ${TOOLCHAIN_FILE}  \
        -DCMAKE_BUILD_TYPE=${BUILD_TYPE} \
	${WEBKIT} \
	${SHARED} \
	-DENABLE_TOOLS=ON \
	-DENABLE_MINIBROWSER=ON \
	-DENABLE_WEBGL=OFF \
	-DUSE_WEBP=OFF -DUSE_CURL=1 \
	-DENABLE_VIBRATION=OFF \
	-DENABLE_GEOLOCATION=OFF \
	-DENABLE_PLUGIN_PROCESS=OFF \
	-DENABLE_NETSCAPE_PLUGIN_API=OFF \
	-DENABLE_PLUGIN_PROCESS=ON \
	-DENABLE_PLUGIN_PROCESS_GTK2=OFF \
	-DENABLE_INTROSPECTION=OFF \
	-DENABLE_NOTIFICATIONS=OFF \
	-DENABLE_METER_ELEMENT=OFF \
	-DENABLE_MEMORY_SAMPLER=OFF \
	-DSATURATED_LAYOUT_ARITHMETIC=OFF \
	-DENABLE_PUBLIC_SUFFIX_LIST=OFF \
	-DENABLE_RESOURCE_USAGE=OFF \
	-DENABLE_WEBDRIVER=OFF \
	-DENABLE_CONTEXT_MENUS=OFF \
	-DENABLE_SQL_DATABASE=OFF \
	-DENABLE_INDEXED_DATABASE=OFF \
	-DENABLE_CREDENTIAL_STORAGE=OFF \
	-DENABLE_VIDEO_TRACK=OFF \
	-DBATTERY_STATUS=OFF \
	-DENCRYPTED_MEDIA_V2=OFF \
	-DENABLE_3D_RENDERING=OFF \
	-DUSE_LIBNOTIFY=OFF \
	-DUSE_LIBHYPHEN=OFF \
	-DUSE_LIBSECRET=OFF \
	-DUSE_WOFF2=OFF \
	-DUSE_XDGMIME=OFF \
	-DUSE_LD_GOLD=OFF \
	-DENABLE_GLES2=OFF \
	-DENABLE_OPENGL=OFF \
	-DENABLE_ACCESSIBILITY=OFF \
	-DHAVE_ACCESSIBILITY=0 \
	-DENABLE_JIT=ON \
	-DENABLE_FTL_JIT=OFF \
	-DENABLE_DFG_JIT=OFF \
	-DENABLE_ICONDATABASE=OFF \
	-DENABLE_3D_TRANSFORMS=OFF \
	-DENABLE_XSLT=OFF \
	-DENABLE_MATHML=OFF \
	-DENABLE_MHTML=OFF \
	-DENABLE_INSPECTOR=OFF \
	-DENABLE_REMOTE_INSPECTOR=OFF \
	-DENABLE_WEB_AUDIO=OFF \
	-DENABLE_WEBASSEMBLY=OFF \
	-DENABLE_CHANNEL_MESSAGING=OFF \
	-DENABLE_DRAG_SUPPORT=OFF \
	-DENABLE_TOUCH_EVENTS=ON \
	-DENABLE_VIDEO=OFF \
	-DENABLE_CDROID_VIDEO=OFF \
	-DUSE_GSTREAMER_GL=OFF \
	-DUSE_THIN_ARCHIVES=OFF \
	-DENABLE_SAMPLING_PROFILER=OFF \
	-DENABLE_SPELLCHECK=OFF \
	-DENABLE_STREAMS_API=OFF \
	-DENABLE_SUBTLE_CRYPTO=OFF \
	-DENABLE_SVG=ON \
	-DENABLE_SVG_FONTS=ON \
	-DENABLE_FETCH_API=OFF \
	-DENABLE_USERSELECT_ALL=OFF \
	-DENABLE_X11_TARGET=OFF \
	-DENABLE_WAYLAND_TARGET=OFF \
	-DENABLE_USER_MESSAGE_HANDLERS=OFF \
	-DSATURATED_LAYOUT_ARITHMETIC=OFF \
	.. 
#-G Ninja

